/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import DTO.Object.OrderUser.Orderuser1;
import DTO.Object.OrderUser.Orderusers;
import DTO.OrderUser;
import Model.marshallXML;
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author annt60258
 */
public class ControllerOrderservlet extends HttpServlet {
   List listorder=null;
   Orderusers orderusers;
   String idorder = "";
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            HttpSession session = request.getSession();
            String btaction = request.getParameter("btSearch");
            if(btaction.equals("Search"))
            {
                listorder = new ArrayList();
            orderusers = new Orderusers();
            String search = request.getParameter("txtSearch");
            String username = session.getAttribute("username").toString();
            String realpath = getServletContext().getRealPath("/");
            String filepath = realpath + "XML/Orderxml.xml";
            File file = new File(filepath);
            //goi ham marshall de co file xml
            marshallXML.marshallOrder(filepath, username);
            //parse xml thanh cay dom trc khi search
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            //bat dau search
            SearchOrder(doc,search,username);
            
            session.setAttribute("action", listorder);
            RequestDispatcher rqd = request.getRequestDispatcher("PrinApllyXsl.jsp");
            rqd.forward(request, response);
            }

        }  catch (SAXException ex) {
            Logger.getLogger(ControllerOrderservlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ControllerOrderservlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        
    }

    public void SearchOrder(Node node, String search,String username)
    {
        if(node==null) return; 
        if(node.getNodeName().equals("OrderID"))
        {
            idorder = node.getTextContent().trim();
        }
        if(node.getNodeName().equals("OrderDate"))
        {
            if(node.getTextContent().trim().replace("T", " ").indexOf(search)>-1)
            {
                
                    String dateorder = node.getTextContent().trim();
                    Node sibling = node.getNextSibling();
                    while (!sibling.getNodeName().equals("OrderStatus")) {
                        sibling = sibling.getNextSibling();
                    }
                    String statusorder = sibling.getTextContent().trim();

                    OrderUser orderuser = new OrderUser(idorder, username, dateorder, statusorder);
                    listorder.add(orderuser);
                
            }
        }
        NodeList nodelist = node.getChildNodes();
        for(int i =0;i<nodelist.getLength();i++)
        {
            SearchOrder(nodelist.item(i), search, username);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
